package com.levy.jituan;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.levy.utils.JsonUtil1;
import javafx.embed.swing.JFXPanel;

import java.net.URL;
import java.sql.Struct;

public class ThreadGetLocation implements Runnable {

    private DataQueue dataQueue;

    private String desPath;

    public void setDesPath(String desPath) {
        this.desPath = desPath;
    }

    public static volatile boolean over = false;

    public DataQueue getDataQueue() {
        return dataQueue;
    }

    public void setDataQueue(DataQueue dataQueue) {
        this.dataQueue = dataQueue;
    }

    String getLocationUrl = "https://restapi.amap.com/v3/geocode/geo?key=$Key&city=$City&address=$Addr";

    private static String key = KeyUtils.getRandomKey();


    @Override
    public void run() {
        int j = 0;
        long l = System.currentTimeMillis();
        Thread thread = Thread.currentThread();
        Company errCompany = null;
        try {
            while (!over) {
                int queueSize = dataQueue.getQueueSize();
                if (queueSize == 0) {
                    return;
                }
                System.out.println("-------------------------------------------------------------");
                Company oneCompany = dataQueue.getOneCompany();
                errCompany = oneCompany;
                String addr = oneCompany.getAttrValue1();
                String city = StrUtil.isNotBlank(oneCompany.getCity()) ? oneCompany.getCity() : "";
                if (StrUtil.isNotBlank(addr)) {
                    String url = "";
                    try {
                        url = getLocationUrl.replaceAll("\\$Addr", addr);
                        url = url.replaceAll("\\$City", city);
                        if (StrUtil.isBlank(key)) {
                            key = KeyUtils.getRandomKey();
                        }
                        url = url.replaceAll("\\$Key", key);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(url);
                    String s = HttpUtil.get(url);
                    if (StrUtil.isNotBlank(s)) {
                        JSONObject jsonObject = JSONUtil.parseObj(s);
                        if ("1".equals(jsonObject.get("status"))) {
                            JSONArray geocodes = jsonObject.getJSONArray("geocodes");
                            if (geocodes != null && geocodes.size() > 0) {
                                JSONObject j0 = geocodes.getJSONObject(0);
                                if (j0 != null) {
                                    String location = j0.getStr("location");
                                    if (StrUtil.isNotBlank(location)) {
                                        String[] split = location.split(",");
                                        oneCompany.setGcj02Lon(split[0]);
                                        oneCompany.setGcj02Lat(split[1]);
                                    }
                                }
                            }
                        } else {
                            String infocode = jsonObject.getStr("infocode");
                            if ("10003".equals(infocode) || "10004".equals(infocode)) {
                                System.out.println("----------error key-----------");
                                System.out.println(key);
                                KeyUtils.removeKey(key);
                                key = KeyUtils.getRandomKey();
                            } else if ("10020".equals(infocode)) {
                                Thread.sleep(800);
                            }

                            //重试一次
                            String s2 = HttpUtil.get(url);
                            if (StrUtil.isNotBlank(s2)) {
                                JSONObject jsonObject2 = JSONUtil.parseObj(s2);
                                if ("1".equals(jsonObject2.get("status"))) {
                                    JSONArray geocodes = jsonObject2.getJSONArray("geocodes");
                                    if (geocodes != null && geocodes.size() > 0) {
                                        JSONObject j0 = geocodes.getJSONObject(0);
                                        if (j0 != null) {
                                            String location = j0.getStr("location");
                                            if (StrUtil.isNotBlank(location)) {
                                                String[] split = location.split(",");
                                                oneCompany.setGcj02Lon(split[0]);
                                                oneCompany.setGcj02Lat(split[1]);
                                            }
                                        }
                                    }
                                } else {
                                    System.out.println("----------error data-----------");
                                    FileUtil.appendUtf8String(oneCompany.toString(), desPath.replaceAll(".log", "_err.log"));

                                }
                            }

                        }
                    }
                }

                FileUtil.appendUtf8String(oneCompany.toString(), desPath);
                System.out.println("threadName:" + thread.getName() + ", threadId: " + thread.getId() + ", " + j++ + ",reamain enpSize: " + queueSize + "个, company:  " + addr);
                if (queueSize == 0) {
                    over = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("----------error data-----------");
            if (errCompany != null) {
                FileUtil.appendUtf8String(errCompany.toString(), desPath.replaceAll(".log", "_err.log"));
            }

        }

        System.out.println("ThreadName_" + thread.getName() + "  :all over_" + j + "次数");
    }

}
